{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 张量的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2, 3],\n",
       "        [1, 3, 2]],\n",
       "\n",
       "       [[1, 2, 3],\n",
       "        [1, 3, 2]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "np.array(3) #标量\n",
    "np.array([1,2,3]) #向量\n",
    "np.array([[1,2,3],[4,5,6]]) #矩阵\n",
    "np.array([[[1,2,3],[1,3,2]],[[1,2,3],[1,3,2]]]) #3维张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.3412463439666887"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand() #0到1之间采样的随机数（均匀分布上采样）\n",
    "np.random.randn() #在标准正太分布上采样的随机数\n",
    "np.random.randint(1,10) #在[1,10)之间进行整数采样\n",
    "np.random.uniform(1.0,2.0) #均匀分布采样\n",
    "\n",
    "# import random\n",
    "# random.randint(1,3) #不要用，左闭又闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[7, 7, 9, 6, 1],\n",
       "        [6, 9, 9, 9, 8]],\n",
       "\n",
       "       [[9, 5, 8, 8, 5],\n",
       "        [8, 7, 6, 4, 3]],\n",
       "\n",
       "       [[7, 2, 1, 8, 1],\n",
       "        [6, 7, 9, 6, 5]]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(3,2,5)\n",
    "np.random.randint(1,10,size=(3,2,5)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = list(range(10))\n",
    "np.array(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.39450539,  1.04488771,  0.70158437,  0.90973277],\n",
       "       [ 0.        , -0.37091906, -0.45492175, -0.05620024],\n",
       "       [ 0.        ,  0.        ,  0.67101429,  0.91598024],\n",
       "       [ 0.        ,  0.        ,  0.        , -1.14972696]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3,4))\n",
    "np.ones((3,3))\n",
    "np.eye(5)\n",
    "np.tril(np.random.randn(4,4))\n",
    "np.triu(np.random.randn(4,4))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randn(5,6)\n",
    "b = np.zeros_like(a)\n",
    "\n",
    "b,b.shape\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
